var dataWithHeaderToTable = function(result) {
    var data = result.data;
    var tbody = data.tbody;
    var thead = data.thead;
    var columns = [{
        field: 'index',
        align: 'center',
        title: "序号",
        formatter: function (value, row, index) {
            var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
            var columnId = $.common.sprintf("<input type='hidden' name='targets[%s].id' value='%s'>", index, row.id);
            return columnIndex + $.table.serialNumber(index) + columnId;
        }
    }];
    $.each(thead,function(index,item) {
        columns.push({
            field: index,
            title: item
        });
    });
    $("#bootstrap-table").bootstrapTable('destroy');
    var options = {
        data:tbody,
        modalName: "",
        fixedColumns: true,
        fixedNumber: 5,
        height: 700,
        pagination: false,
        columns:columns
    };
    $.table.init(options);
};
function tableDataExport(tableParam) {
    $.ajax({
        url: ctx+'base/etl/query/dw/commonDwQuery/export',
        type: 'post',
        dataType: 'json',
        contentType: 'application/json;charset=UTF-8',
        data:JSON.stringify(tableParam),
        success: function (result) {
            if(result.code==0) {
                window.location.href = ctx + "common/download?fileName=" + encodeURI(result.data) + "&delete=" + true;
            } else {
                $.modal.alertError(result.msg);
            }
        }
    });
}

function dwTableDataInit(tableParam) {
    $.modal.loading("数据加载中，请稍后");
    $.ajax({
        url: ctx+'base/etl/query/dw/commonDwQuery',
        type: 'post',
        dataType: 'json',
        contentType: 'application/json;charset=UTF-8',
        data:JSON.stringify(tableParam),
        success: function (result) {
            if(result.code==0) {
                debugger;
                dataWithHeaderToTable(result);
            } else {
                $.modal.alertError(result.msg);
            }
            $.modal.closeLoading();
        }
    });
}


function dimParamTableDataInit(tableParam) {
    $("#bootstrap-table").bootstrapTable("destroy");
    $.ajax({
        url: ctx+'base/md/dim/param/queryDataByParam',
        type: 'post',
        dataType: 'json',
        contentType: 'application/json;charset=UTF-8',
        data:JSON.stringify(tableParam),
        success: function (result) {
            if(result.code==0) {
                var data = result.data;
                var tbody = data.tbody;
                var thead = data.thead;
                var columns = [{
                    field: 'index',
                    align: 'center',
                    title: "序号",
                    formatter: function (value, row, index) {
                        var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                        var columnId = $.common.sprintf("<input type='hidden' name='targets[%s].id' value='%s'>", index, row.id);
                        return columnIndex + $.table.serialNumber(index) + columnId;
                    }
                }];
                $.each(thead,function(index,item) {
                    if(item.indexOf('日期')>-1) {
                        debugger;
                        columns.push({
                            field: index,
                            title: item,
                            formatter: function(value, row, index) {
                                if(value!=undefined && value.length>3) {
                                    return value[0]+'-'+value[1]+'-'+value[2];
                                } else {
                                    return '';
                                }
                            }
                        });
                    } else if(item.indexOf('手机号')>-1) {
                        debugger;
                        columns.push({
                            field: index,
                            title: item,
                            formatter: function(value, row, index) {
                                if(value!=null &&value.length>10) {
                                    return value.replace(/^(.{4})(.*)(.{4})$/, '$1****$3');
                                } else {
                                    return value;
                                }
                            }
                        });
                    } else {
                        columns.push({
                            field: index,
                            title: item
                        });
                    }
                });
                var options = {
                    data:tbody,
                    modalName: "",
                    fixedColumns: true,
                    fixedNumber: 5,
                    height: 700,
                    pagination: false,
                    columns:columns
                };
                $.table.init(options);
            } else {
                $.modal.alertError(result.msg);
            }
        }
    });
}

function dimParamTableDataExport(tableParam) {
    $.ajax({
        url: ctx+'base/md/dim/param/queryDataByParam/export',
        type: 'post',
        dataType: 'json',
        contentType: 'application/json;charset=UTF-8',
        data:JSON.stringify(tableParam),
        success: function (result) {
            if(result.code==0) {
                window.location.href = ctx + "common/download?fileName=" + encodeURI(result.data) + "&delete=" + true;
            } else {
                $.modal.alertError(result.msg);
            }
        }
    });
}